#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018.11.17 11:44 
# @Author : By Joker
# @File : listening_key_01
# @Software: PyCharm
# @desc: 可以同时监听鼠标.键盘动作
import PyHook3

import pythoncom


def on_mouse_event(event):
    """
    监听鼠标事件
    :param event:
    :return: 返回True,以便将事件传给其他处理程序
              注意, 若返回False, 则鼠标事件将被全部拦截
              导致鼠标失去响应
    """
    print('================== start listening mouse ====================')
    print('MessageName: ', event.MessageName)
    print('Message: ', event.Message)
    print('Time: ', event.Time)
    print('Window: ', event.WindowName)
    print('Position: ', event.Position)
    print('Wheel: ', event.Wheel)
    print('Injected: ', event.Injected)
    print('================== end listening mouse ====================')

    return True


def on_keyboard_event(event):
    """
    监听键盘事件
    :param event:
    :return: 同鼠标监听事件的返回值
    """

    print('================== start listening keyboard ====================')
    print('MessageName: ', event.MessageName)
    print('Message: ', event.Message)
    print('Time: ', event.Time)
    print('Window: ', event.Window)
    print('Ascii: ', event.Ascii, chr(event.Ascii))
    print('Key: ', event.Key)
    print('KeyID: ', event.KeyID)
    print('ScanCode: ', event.ScanCode)
    print('Extended: ', event.Extended)
    print('Injected: ', event.Injected)
    print('Alt: ', event.Alt)
    print('Transition: ', event.Transition)
    print('================== start listening keyboard ====================')

    return True


def main():
    # 创建监听对象
    manager = PyHook3.HookManager()

    # 监听所有键盘事件
    manager.KeyDown = on_keyboard_event
    # 设置键盘监听
    manager.HookKeyboard()

    # 监听鼠标事件
    manager.MouseAll = on_mouse_event
    # 设置鼠标监听
    manager.HookMouse()

    # 循环监听, 若不手动关闭, 程序将一直处于监听状态
    pythoncom.PumpMessages()


if __name__ == '__main__':
    main()
